package com.dianrui.agent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.Manager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ManagerAdapter extends BaseAdapter {

    Context context;
    List<Manager>fixedList;
    ManagerClickListener managerClickListener;

    public ManagerAdapter(Context context, List<Manager> fixedList) {
        this.context = context;
        this.fixedList = fixedList;
    }

    public void refresh(List<Manager>fixLists) {
        this.fixedList = fixLists;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return fixedList.size();
    }

    @Override
    public Object getItem(int position) {
        return fixedList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface ManagerClickListener {
        void onManagerClick(String mobile);

    }

    public void setOnManagerClickListener(ManagerClickListener managerClickListener) {
        this.managerClickListener = managerClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_manger, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (fixedList.size()>0) {
            final Manager manager = fixedList.get(position);
            holder.showName.setText(manager.account+"("+manager.role_name+")");
            holder.showMobile.setText(manager.mobile);
            holder.managerLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (managerClickListener != null) {
                        managerClickListener.onManagerClick(manager.mobile);
                    }

                }
            });


        }
        return convertView;
    }
    class ViewHolder {
        @BindView(R.id.show_mobile)
        TextView showMobile;
        @BindView(R.id.show_name)
        TextView showName;
        @BindView(R.id.call_img)
        ImageView callImg;
        @BindView(R.id.manager_layout)
        LinearLayout managerLayout;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
